package com.zw.kafka;

import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.CreateTopicsResult;
import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.clients.producer.ProducerConfig;

import java.util.ArrayList;
import java.util.Properties;
import java.util.concurrent.ExecutionException;

/**
 * @author morningstar
 * @date 2021/12/5
 * @desc
 */
public class CreateTopic {
    public static void main(String[] args) {
        // 1.设置参数
        Properties props = new Properties();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "tajlmt0law.cogiot.net:39092,tajlmt0law.cogiot.net:39093,tajlmt0law.cogiot.net:39094");
        AdminClient adminClient = AdminClient.create(props);
        ArrayList<NewTopic> topics = new ArrayList<NewTopic>();
        /**
         * 1.String name: 主题名
         * 2.int numPartitions: 分区数
         * 3.short replicationFactor: 副本数
         */
        NewTopic newTopic = new NewTopic("test", 3, (short) 3);
        topics.add(newTopic);
        CreateTopicsResult result = adminClient.createTopics(topics);

        try {
            result.all().get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }
}
